﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeachingManageSystem.BLL;
using TeachingManageSystem.Entity;

namespace TeachingManageSystem.Web.Users
{
    public partial class AddUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                GetDropdownlist();
                GetGridview();
            }
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnAddUserClick(object sender, EventArgs e)
        {
            var user = new User {UserNum = Convert.ToInt32(TbxUsereNum.Text), Name = TbxUserName.Text,UsersTypeID =Convert.ToInt32(DrpRoles.SelectedValue)};
            UsersBll.InsertUsers(user);
            GetGridview();
        }
        /// <summary>
        /// 绑定GridView
        /// </summary>
        public  void GetGridview()
        {
            GridViewUserAndRole.DataSource = UsersBll.GetAllUsers();
            GridViewUserAndRole.DataBind();
        }
        /// <summary>
        /// 绑定Dropdownlist
        /// </summary>
        public void GetDropdownlist()
        {
            DrpRoles.DataSource = UsersTypeBll.GetAllUsersType();
            DrpRoles.DataBind();
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridViewUserAndRolePageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewUserAndRole.PageIndex = e.NewPageIndex;
            GetGridview();
        }

        protected void GridViewUserAndRoleRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var key = GridViewUserAndRole.DataKeys[e.RowIndex];
            if(key!=null)
            {
                var dataKey = key.Value.ToString();
                {
                    var id = Convert.ToInt32(dataKey);
                    UsersBll.DeleteUsersByID(id);
                }
                GetGridview();
            }
        }

       

        
    }
}